export default {
    inject: ['getGlobalDsv'],
    methods: {
        appendTableRow(widget) {
            this.designer.appendTableRow(widget)
        },

        appendTableCol(widget) {
            this.designer.appendTableCol(widget)
        },

        onContainerDragAdd(evt, subList) {
            const newIndex = evt.newIndex
            if (subList[newIndex]) {
                this.designer.setSelected(subList[newIndex])
            }

            this.designer.emitHistoryChange()
            this.designer.emitEvent('field-selected', this.widget)
        },

        onContainerDragUpdate() {
            this.designer.emitHistoryChange()
        },

        checkContainerMove(evt) {
            return this.designer.checkWidgetMove(evt)
        },

        selectWidget(widget) {
            if (this.designer.startSelect) {
                let loop = (data) => {
                    let {type} = data;
                    let ids = this.designer.selfComponent.map(item => item.id);
                    if (type === "grid") {
                        data.cols.forEach(item => {
                            if (item.category && item.category === "container") {
                                loop(item)
                            } else {
                                if (!ids.includes(item.id)) {
                                    this.designer.selfComponent.push(item)
                                } else {
                                    let i = ids.indexOf(item.id)
                                    this.designer.selfComponent.splice(i, 1)
                                }
                            }
                        })
                    } else if (type === "tab") {
                        data.tabs.forEach(item => {
                            if (item.category && item.category === "container") {
                                loop(item)
                            } else {
                                if (!ids.includes(item.id)) {
                                    this.designer.selfComponent.push(item)
                                } else {
                                    let i = ids.indexOf(item.id)
                                    this.designer.selfComponent.splice(i, 1)
                                }
                            }
                        })
                    } else if (type === "el-dialog" || type === "el-drawer" || type === "grid-col" || "tab-pane") {
                        data.widgetList.forEach(item => {
                            if (item.category && item.category === "container") {
                                loop(item)
                            } else {
                                if (!ids.includes(item.id)) {
                                    this.designer.selfComponent.push(item)
                                } else {
                                    let i = ids.indexOf(item.id)
                                    this.designer.selfComponent.splice(i, 1)
                                }
                            }
                        })
                    }
                    if (!data.category) {
                        if (!ids.includes(data.id)) {
                            this.designer.selfComponent.push(data)
                        } else {
                            let i = ids.indexOf(j.id)
                            this.designer.selfComponent.splice(i, 1)
                        }
                    }
                }
                loop(widget);
            }
            this.designer.setSelected(widget)
        },

        selectParentWidget() {
            if (this.parentWidget) {
                this.designer.setSelected(this.parentWidget)
            } else {
                this.designer.clearSelected()
            }
        },

        moveUpWidget() {
            this.designer.moveUpWidget(this.parentList, this.indexOfParentList)
            this.designer.emitHistoryChange()
        },

        moveDownWidget() {
            this.designer.moveDownWidget(this.parentList, this.indexOfParentList)
            this.designer.emitHistoryChange()
        },

        cloneContainer(widget) {
            if (this.parentList) {
                let newCon = this.designer.cloneContainer(widget)
                this.parentList.splice(this.indexOfParentList + 1, 0, newCon)
                this.designer.setSelected(newCon)

                this.designer.emitHistoryChange()
            }
        },

        removeWidget() {
            if (this.parentList) {
                const widgetRefName = this.designer.selectedWidgetName
                let nextSelected = null
                if (this.parentList.length === 1) {
                    if (this.parentWidget) {
                        nextSelected = this.parentWidget
                    }
                } else if (this.parentList.length === (1 + this.indexOfParentList)) {
                    nextSelected = this.parentList[this.indexOfParentList - 1]
                } else {
                    nextSelected = this.parentList[this.indexOfParentList + 1]
                }

                this.$nextTick(() => {
                    this.parentList.splice(this.indexOfParentList, 1)
                    this.designer.setSelected(nextSelected)

                    this.designer.formWidget.deleteWidgetRef(widgetRefName)  //删除组件ref！！！
                    this.designer.emitHistoryChange()
                })
            }
        },

        setWidgetOption(optionName, optionValue) { //通用组件选项修改API
            if (this.widget.options.hasOwnProperty(optionName)) {
                this.widget.options[optionName] = optionValue
            }
        },

    }
}
